package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.MallGoodsBrandEntity;
import com.qf.service.impl.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/brand")
@SuppressWarnings("all")
@CrossOrigin
@Api(tags = "品牌控制器")
public class BrandController {
    @Autowired
    private BrandService service;

    @PostMapping("/find")
    @ApiOperation("分页查询品牌信息")
    public PageInfo<MallGoodsBrandEntity> findByPage(@ApiParam("当前页，不给则默认为1") @RequestParam(value = "page", defaultValue = "1") String page,@ApiParam("分页大小，默认为1") @RequestParam(value = "size", defaultValue = "1") String size,@ApiParam("条件查询对象") @RequestBody MallGoodsBrandEntity entity) {
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(size));
        List<MallGoodsBrandEntity> byPage = service.findByPage(entity);
        return new PageInfo<MallGoodsBrandEntity>(byPage);
    }

    @PostMapping("/update")
    @ApiOperation("修改品牌信息")
    public HashMap<String, String> update(@ApiParam("修改数据对象")@RequestBody MallGoodsBrandEntity entity) {
        int update = service.update(entity);
        if (update == 1) {
            return new HashMap<String, String>() {{
                put("esg", "修改成功");
            }};
        }
        return new HashMap<String, String>() {{
            put("esg", "修改失败");
        }};
    }

    @PostMapping("/add")
    @ApiOperation("添加品牌")
    public HashMap<String, String> add(@ApiParam("新增数据对象")@RequestBody MallGoodsBrandEntity entity) {
        int add = service.add(entity);
        if (add == 1) {
            return new HashMap<String, String>() {{
                put("esg", "添加成功");
            }};
        }
        return new HashMap<String, String>() {{
            put("esg", "添加失败");
        }};
    }

    @PostMapping("/delete")
    @ApiOperation("删除品牌")
    public HashMap<String, String> delete(@ApiParam("删除id")@RequestParam(value = "id") String id) {
        int delete = service.delete(Integer.parseInt(id));
        if (delete == 1) {
            return new HashMap<String, String>() {{
                put("esg", "删除成功");
            }};
        }
        return new HashMap<String, String>() {{
            put("esg", "删除失败");
        }};
    }
}
